package com.elvin.zoe.parser.sms;

import java.util.ArrayList;
import java.util.List;

import com.elvin.zoe.pojo.Message;

public class SmsTxtractParser extends SmsParser {

    @Override
    protected List<String> getMessageTokens(String originalHtml) {
        String messageStartToken = "<div class=\"message ";
        String messageEndToken = "</div>";
        List<String> messageTokens = new ArrayList<String>();
        int tokenStart = originalHtml.indexOf(messageStartToken);
        while (tokenStart > 0) {
            int tokenEnd = originalHtml.indexOf(messageEndToken, tokenStart) + messageEndToken.length();
            messageTokens.add(originalHtml.substring(tokenStart, tokenEnd));
            tokenStart = originalHtml.indexOf(messageStartToken, tokenEnd);
        }
        return messageTokens;
    }

    @Override
    protected int getMessageType(String messageToken) {
        if (messageToken.indexOf("message me") > 0)
            return Message.OUTGOING;
        else
            return Message.INCOMMING;
    }

    @Override
    protected String getMessageDatetime(String messageToken) {
        String startToken = "<span class=\"datetime\">";
        int tokenStart = messageToken.indexOf(startToken) + startToken.length();
        int tokenEnd = messageToken.indexOf("</span>", tokenStart);
        return messageToken.substring(tokenStart, tokenEnd);
    }

    @Override
    protected String getMessageDatetimeFormat() {
        return "MMM d, yyyy hh:mm:ss aaa";
    }

    @Override
    protected String getMessageText(String messageToken) {
        List<String> messageTexts = new ArrayList<String>();
        getNextMessageText(messageTexts, messageToken, 0);

        String finalMessage = "";
        for(String messageText : messageTexts){
            finalMessage += messageText + " ";
        }
        return finalMessage;
    }

    private void getNextMessageText(List<String> messageTexts, String messageToken, int start) {
        String startToken = "<span class=\"txtmsg\">";
        int tokenStart = messageToken.indexOf(startToken, start) + startToken.length();
        int tokenEnd = messageToken.indexOf("</span>", tokenStart);

        if (tokenStart <= startToken.length() + start - 1)
            return;

        String messageText = messageToken.substring(tokenStart, tokenEnd).trim();
        if (messageText.length() > 0 && ! messageTexts.contains(messageText))
            messageTexts.add(messageText);

        getNextMessageText(messageTexts, messageToken, tokenEnd);
    }

    @Override
    protected String getMessageImageName(String messageToken) {
        String startToken = "<img src=\"./"; //114/
        int tokenStart1 = messageToken.indexOf(startToken) + startToken.length();
        int tokenStart = messageToken.indexOf("/", tokenStart1) + 1 ;
        if(tokenStart1 <= startToken.length() -1)
            return null;
        int tokenEnd = messageToken.indexOf("\"", tokenStart);
        return messageToken.substring(tokenStart, tokenEnd);
    }


}
